{extend name="../../../view/public/base" /}

{block name="body"}
<div class="bw-container">
    <div class="bw-main">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <!--搜索区域-->
                <div class="layui-card">
                    <div class="layui-collapse" lay-accordion="" lay-filter="search">
                        <div class="layui-colla-item">
                            <h2 class="layui-colla-title">查询条件</h2>
                            <div class="layui-colla-content">
                                <form id="searchForm" class="layui-form" lay-filter="searchForm" autocomplete="off">

                                    <div class="layui-form-item layui-inline">
                                        <label class="layui-form-label">ID</label>
                                        <div class="layui-input-inline">
                                            <input type="number" name="id" placeholder="请输入ID"
                                                   class="layui-input">
                                        </div>
                                    </div>

                                    <div class="layui-form-item">
                                        <div class="layui-input-block">
                                            <button class="layui-btn layui-btn-normal" type="button" lay-submit lay-filter="searchBtn">
                                                搜索
                                            </button>
                                            <button class="layui-btn layui-btn-primary" type="button" onclick="resetForm()">
                                                重置
                                            </button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

                <!--列表区域-->
                <div class="layui-card" style="background-color: #F2F2F2;">
                    <table id="list" lay-filter="listFilter"></table>

                    <!--工具栏-->
                    <script type="text/html" id="listToolbar">
                        <a class="layui-btn layui-btn-sm layui-bg-black" lay-event="refresh" title="刷新"><i class="layui-icon layui-icon-refresh" style="font-size: 8px !important;"></i> </a>
                        {Auth:node name='/manage/admin.member/add'}
                        <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add" title="新增">新增顶级租户</a>
                        {/Auth:node}
                        {Auth:node name='/manage/admin.member/del'}
                        <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del" title="删除">删除</a>
                        {/Auth:node}
                    </script>

                    <!--应用id-->
                    <script type="text/html" id="bind_member_miniapp_id">
                        {{ d.bind_member_miniapp_id }}
                    </script>
                    <!--上级id-->
                    <script type="text/html" id="parent_id">
                        {{ d.parent_id }}

                    </script>
                    <!--手机号-->
                    <script type="text/html" id="mobile">
                        {{ d.mobile }}
                    </script>
                    <!--昵称-->
                    <script type="text/html" id="nickname">
                        {{ d.nickname }}

                    </script>
                    <!--头像-->
                    <script type="text/html" id="avatar">
                        {{ getImagesHtmlForIndex(d.avatar) }}
                    </script>
                    <!--密码-->
                    <script type="text/html" id="password">
                        {{ d.password }}
                    </script>
                    <!--用户名-->
                    <script type="text/html" id="username">
                        {{ d._name }}
                        {{# if (d.is_top ==true) { }}
                        <span class="layui-badge">顶级租户</span>
                        {{# } else { }}
                        <span class="layui-badge layui-bg-blue">子租户</span>
                        {{# } }}
                    </script>
                    <!--安全密码-->
                    <script type="text/html" id="safe_password">
                        {{ d.safe_password }}
                    </script>
                    <!--状态:0=正常,1=冻结-->
                    <script type="text/html" id="is_lock">
                        <input type="checkbox" {{ d.status == '1' ? 'checked' : '' }} name="is_lock" value="{{d.id}}" lay-skin="switch" lay-filter="is_lockChange" lay-text="正常|冻结">
                    </script>
                    <!--配置权限:0=开启,1=关闭-->
                    <script type="text/html" id="lock_config">
                        {{# if (d.lock_config == '0') { }}
                        <span style="color: #1E9FFF;">开启</span>
                        {{# } else if(d.lock_config == '1') { }}
                        <span style="color: #1E9FFF;">关闭</span>
                        {{# } else { }}
                        <span style="color: #1E9FFF;">{{d.lock_config}}</span>
                        {{# } }}
                    </script>
                    <!--登录ip-->
                    <script type="text/html" id="login_ip">
                        {{ d.login_ip }}
                    </script>
                    <!--登录时间-->
                    <script type="text/html" id="login_time">
                        {{ formatDate(d.login_time) }}
                    </script>
                    <!--创建时间-->
                    <script type="text/html" id="create_time">
                        {{ formatDate(d.create_time) }}
                    </script>
                    <!--更新时间-->
                    <script type="text/html" id="update_time">
                        {{ formatDate(d.update_time) }}
                    </script>
                    <script type="text/html" id="auth_group_access">
                        {{#  layui.each(d.auth_group_access, function(index, item){ }}
                        <span class="layui-badge layui-bg-orange">{{ item}}</span>
                        {{#  }); }}
                    </script>
                    <!--状态开关-->
                    <script type="text/html" id="listBar">
                        {Auth:node name='/manage/admin/message/send'}
                        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="send">发送站内信</a>
                        {/Auth:node}

                        {Auth:node name='/manage/admin.member/edit'}
                        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑</a>
                        {/Auth:node}

                        {{# if (d.is_top == true) { }}
                        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="user">租户角色组</a>
                        {{# } }}

                        {Auth:node name='/manage/admin.member/del'}
                        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
                        {/Auth:node}
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

{/block}

{block name="js"}
<script>
    var element, form, table, bwajax;

    layui.use(['element', 'form', 'table', 'bwajax'], function () {
        element = layui.element;
        form = layui.form;
        table = layui.table;
        bwajax = layui.bwajax.instance();

        //执行列表
        table.render({
            elem: '#list',
            url: "/manage/admin/member/index",
            response: {
                statusCode: 200,  //规定成功的状态码，默认：0
            },
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.data.total, //解析数据长度
                    "data": res.data.data.list //解析数据列表
                };
            },
            toolbar: '#listToolbar',
            cols: [[
                {type:'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', align: 'center', width: 80, fixed: 'left'},
                {field: 'bind_member_miniapp_id', title: '应用id', align: 'center', width: 80, templet: '#bind_member_miniapp_id',hide: true},
                {field: 'parent_id', title: '上级id', align: 'center', width: 80, templet: '#parent_id',hide: true},
                {field: '_name', title: '用户名',  minWidth: 200, templet: '#username'},
                {field: 'mobile', title: '手机号', align: 'center', minWidth: 150, templet: '#mobile'},
                {field: 'nickname', title: '昵称', align: 'center', minWidth: 150, templet: '#nickname'},
                {field: 'avatar', title: '头像', align: 'center', width: 60, templet: '#avatar'},
                {field: 'password', title: '密码', align: 'center', minWidth: 150, templet: '#password',hide: true},
                {field: 'safe_password', title: '安全密码', align: 'center', minWidth: 150, templet: '#safe_password',hide: true},
                {field: 'lock_config', title: '配置权限', align: 'center', width: 100, templet: '#lock_config'},
                {field: 'auth_group_access', title: '拥有角色组', align: 'center',minWidth: 300, templet: '#auth_group_access'},
                {field: 'login_ip', title: '登录ip', align: 'center', minWidth: 150, templet: '#login_ip'},
                {field: 'login_time', title: '登录时间', align: 'center', width: 160, templet: '#login_time'},
                {field: 'create_time', title: '创建时间', align: 'center', width: 160, templet: '#create_time'},
                {field: 'update_time', title: '更新时间', align: 'center', width: 160, templet: '#update_time',hide: true},
                {field: 'is_lock', title: '状态', align: 'center', width: 100, templet: '#is_lock'},

                {title: '操作', toolbar: '#listBar', align: 'center', fixed: "right", width: 280}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 100,
            page: true
        });

        //监听表格头部工具栏事件
        table.on('toolbar(listFilter)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'refresh':
                    reload();
                    break;
                case 'add':
                    bwOpenIframe("/manage/admin/member/add", '新增');
                    break;
                case 'del':
                    var checkStatus = table.checkStatus('list');
                    var data = checkStatus.data;

                    var idsArr = [];
                    data.forEach(function(value) {
                        idsArr.push(value.id);
                    });
                    ids = idsArr.join(',');

                    if(!ids) layer.msg('没有选中行', {icon: 2});
                    else del(ids);

                    break;

            }
        });

        //监听工具条
        table.on('tool(listFilter)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

            switch (layEvent) {
                case 'edit':
                    bwOpenIframe("/manage/admin/member/edit?id=" + data.id, '编辑');
                    break;
                case 'del':
                    del(obj.data.id);
                    break;

                case 'user':
                    layer.open({
                        title: '租户角色管理',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['90%', '90%'],
                        content: '/manage/Role/memberRole?member_id='+obj.data.id,
                    });
                    break;
                case 'send':
                    var content = "<textarea id='msg' placeholder=\"请输入内容\" class=\"layui-textarea\"></textarea>";

                    layer.open({
                        title: '发送站内信',
                        type: 1,
                        area: ['300px', '200px'],
                        closeBtn: 0,
                        content: content
                        ,shadeClose: true
                        ,btn: ['发送']
                        , btnAlign: 'c' //按钮居中
                        ,yes: function(index, layero){
                            var msg = $('#msg').val();
                            if(!msg) {
                                layer.msg('内容不能为空', {icon: 2, time:1000});
                                return;
                            }

                            var data = {
                                member_ids: obj.data.id,
                                msg: msg,
                            };
                            bwajax.post("{:Url('/manage/admin/message/send')}", data)
                                .then(function (response) {
                                    if (response.data.data.errcode === 0) {
                                        layer.msg('发送成功', {icon: 1});
                                    } else layer.msg(response.data.msg, {icon: 2});
                                })
                            layer.close(index);
                        }
                    });
                    break;
            }
        });

        form.on('submit(searchBtn)', function (data) {
            reload(1);

            return false;
        })

        //监听switch开关
        form.on('switch(is_lockChange)', function(obj){
            var state = this.checked ? '0' : '1';

            var id = obj.value;
            bwajax.post("/manage/admin/member/setStatus?id="+id, {id:id,is_lock:state})
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg('修改成功', {icon: 1});
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                        reload();
                    }
                })
        });
    });

    //删除
    function del(ids) {
        layer.confirm('确认删除?', function (index) {
            //ajax调用后台接口
            bwajax.post("/manage/admin/member/del?ids=" + ids)
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg('删除成功', {icon: 1});
                        reload();
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                    }
                });
            layer.close(index);
        });
    }
</script>
{/block}